﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator___WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(
                ((e.KeyChar == (char)45) && textBox1.Text == "")
                || ((e.KeyChar == (char)46) && (textBox1.Text.IndexOf(".") == -1))
                || ((e.KeyChar > (char)47) && (e.KeyChar < (char)58))
                || (e.KeyChar == (char)8)
             ))
            {
                e.Handled = true;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(
                ((e.KeyChar == (char)45) && textBox2.Text == "")
                || ((e.KeyChar == (char)46) && (textBox2.Text.IndexOf(".") == -1))
                || ((e.KeyChar > (char)47) && (e.KeyChar < (char)58))
                || (e.KeyChar == (char)8)
             ))
            {
                e.Handled = true;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = comboBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "" && comboBox1.SelectedIndex != -1)
            {
                float operand1 = float.Parse(textBox1.Text);
                float operand2 = float.Parse(textBox2.Text);
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        label3.Text = Convert.ToString(operand1 + operand2);
                        break;
                    case 1:
                        label3.Text = Convert.ToString(operand1 - operand2);
                        break;
                    case 2:
                        label3.Text = Convert.ToString(operand1 * operand2);
                        break;
                    case 3:
                        label3.Text = (operand2 != 0) ? (Convert.ToString(operand1 / operand2)) : "除数不能为零";
                        break;
                }
            }
        }
    }
}
